{
 "metadata": {
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.5-final"
  },
  "orig_nbformat": 2,
  "kernelspec": {
   "name": "Python 3.8.5 32-bit",
   "display_name": "Python 3.8.5 32-bit",
   "metadata": {
    "interpreter": {
     "hash": "1e54afad9e0c6a58aada5ea9d1dae970e2cf296d72ca4dadc99f4f49825692bb"
    }
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2,
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def quick_sort_pythonic(collection):\n",
    "    length = len(collection)\n",
    "    if length <= 1:\n",
    "        return collection\n",
    "    else:\n",
    "        pivot = collection[0] # 取第一个数作为基准值\n",
    "        left = [element for element in collection[1:] if element <= pivot]\n",
    "        right = [element for element in collection[1:] if element > pivot]\n",
    "        return quick_sort_pythonic(left) + [pivot] + quick_sort_pythonic(right)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 116,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "def paixu(a,k):\n",
    "    while True:\n",
    "        if len(a)<k:\n",
    "            print('The sequence is too short to find element k ')\n",
    "            return\n",
    "        a_1 = a[0]\n",
    "        k_1 = 0\n",
    "        k_2 = 0\n",
    "        B = []\n",
    "        L = []\n",
    "        for i in range(len(a)-1):\n",
    "            a_i = a[i+1]\n",
    "            if a_i<a_1:\n",
    "                L.append(a_i)\n",
    "                k_2 += 1\n",
    "            else:\n",
    "                B.append(a_i)\n",
    "                k_1 += 1\n",
    "        if k_1+1>k:\n",
    "            a = B\n",
    "        elif k_1+1==k:\n",
    "            return a_1\n",
    "        else:\n",
    "            a=L\n",
    "            k = k - k_1-1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 119,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "5"
      ]
     },
     "metadata": {},
     "execution_count": 119
    }
   ],
   "source": [
    "paixu([5,1,8,9,4,6,7],5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ]
}